#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 面向对象高级编程：数据封装、继承和多态只是面向对象程序设计中最基础的3个概念
# 使用__slots__
'''class Student(object):
    pass
# 给实例绑定一个属性
s = Student()
s.name = 'Daniel'  # 动态给实例绑定一个属性
print(s.name)'''


class Student(object):
    __slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称

class GraduateStudent(Student):
    pass

s = Student() # 创建新的实例
s.name = 'Michael' # 绑定属性'name'
s.age = 25 # 绑定属性'age'
# ERROR: AttributeError: 'Student' object has no attribute 'score'
'''try:
    s.score = 99
except AttributeError as e:
    print('AttributeError:', e)'''

g = GraduateStudent()
g.score = 99
print('g.score =', g.score)
#除非在子类中也定义__slots__，这样，子类实例允许定义的属性就是自身的__slots__加上父类的__slots__。